import requests
from flask import Flask, render_template, request, redirect, url_for, Response
from flask_cors import CORS
import copy, json

app = Flask(__name__)
CORS(app)
proxies = {
    "http": None,
    "https": None,
}

user_header_dict = {}


@app.route('/')
def index():
    with open("headers.txt", "w") as f_w:
        print(user_header_dict.keys())
        for h in user_header_dict.values():
            headers = {}
            print(h)
            for k, v in h.items():
                if v != "":
                    headers[k] = v
            f_w.write(json.dumps(headers, ensure_ascii=False, indent=4))
            f_w.write("\n")
    return str(len(user_header_dict.keys()))


@app.route('/user', methods=['GET'])
def user():
    headers = request.headers
    headers = dict(headers)
    headers.update({
        "Host": "apipro.qunjielong.com"
    })
    if headers.get("Uid"):
        user_header_dict.update({
            headers.get("Uid"): headers
        })
    print(headers)
    res = requests.get("https://apipro.qunjielong.com/user-biz/user/user_info", proxies=proxies, headers=headers)
    print(res.text)
    return Response(res.text, res.status_code, res.headers, content_type=res.headers['content-type'])


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
